package Model;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import View.PlayerView;

public class CurrentPlayList {
	
	public static CurrentPlayList instance;
	private List<Nummer> list;
	public ArrayList<Nummer> songs = new ArrayList<Nummer>();
	public ArrayList<Nummer> currentSongs = new ArrayList<Nummer>();
	
	public static CurrentPlayList getInstance() {
		if (instance == null) {
			instance = new CurrentPlayList();
		}
		return instance;
	}
	
	public CurrentPlayList(){
		songs =	PlayerModel.getInstance().songs;	
		list = PlayerModel.getInstance().songs;
	}
	
	public void addSong(Nummer song){
		currentSongs.add(song);
	}
	
	public void removeSong(Nummer song){
		currentSongs.remove(song);
	}
	
	public void addSongs(){
		int rows[] = PlayerView.tbSongs.getSelectedRows();
		if(rows.length > 0){
		for(int i= 0; i < rows.length; i++){
			Nummer song = list.get(PlayerView.tbSongs.convertRowIndexToModel(rows[i]));
			addSong(song);
		}
		
		updateTableModels();
	}
	}
	
	public void removeSongs(){
		int rows[] = PlayerView.tbCurrentSongs.getSelectedRows();
		
		for(int i= 0; i < rows.length; i++){
			Nummer song = list.get(PlayerView.tbCurrentSongs.convertRowIndexToModel(rows[i]));
			removeSong(song);
		}
		
		updateTableModels();
	}
	
	public void loadSongsToModel(){
		DefaultTableModel mdlSongs = PlayerView.mdlSongs;
        DefaultTableModel mdlCurrentPL = PlayerView.mdlCurrentPLSongs;
		
		PlayerView.tbCurrentSongs.setModel(mdlCurrentPL);
		PlayerView.tbCurrentSongs.repaint();
		
		for(int i = 0; i < songs.size() ; i++){
			Nummer song = songs.get(i);
			mdlSongs.addRow(new Object[] {song.getNaam(), song.getArtiest(), song.getAlbum()});
		}
		
		PlayerView.tbSongs.setModel(mdlSongs);
		PlayerView.tbSongs.repaint();
	}
	
	public void updateTableModels(){
		DefaultTableModel mdlCurrentPL = PlayerView.mdlCurrentPLSongs;
		
		for(int i = 0; i < mdlCurrentPL.getRowCount(); i++){
			mdlCurrentPL.removeRow(i);
        }
		
		for(int i = 0; i < currentSongs.size() ; i++){
			Nummer song = currentSongs.get(i);
			mdlCurrentPL.addRow(new Object[] {song.getNaam(), song.getArtiest()});
		}
		
		PlayerView.tbCurrentSongs.setModel(mdlCurrentPL);
		PlayerView.tbCurrentSongs.repaint();
	}

}
